package com.changgou.service.impl;

import com.changgou.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付业务逻辑层
 * @author zhengjd
 * @date 2020/5/20 20:55
 */
@Service
public class WeixinPayServiceImpl implements WeixinPayService {



    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.partner}")
    private String partner;

    @Value("${weixin.partnerkey}")
    private String partnerkey;

    @Value("${weixin.notifyurl}")
    private String notifyurl;



    /**
     * 生成微信支付二维码（调用微信支付的统一下单API）
     * @param out_trade_no  订单号
     * @param money   订单金额
     * @return
     */
    @Override
    public Map<String, String> createNative(String out_trade_no, String money) {

        try {
            //设置统一下单请求所需的参数
            HashMap<String, String> paraMap = new HashMap<>();
            paraMap.put("appid",appid);  //公众账号ID
            paraMap.put("mch_id",partner);  //商户号
            paraMap.put("nonce_str", WXPayUtil.generateNonceStr());  //随机字符串
            paraMap.put("body","畅购");   //商品描述
            paraMap.put("out_trade_no",out_trade_no);  //商户订单号
            paraMap.put("total_fee",money);  //标价金额
            paraMap.put("spbill_create_ip","172.0.0.1");  //终端IP
            paraMap.put("notify_url",notifyurl);  //通知地址
            paraMap.put("trade_type","NATIVE");   //交易类型
            //将map转化为xml(并生成数字签名)
            String xml = WXPayUtil.generateSignedXml(paraMap,partnerkey);
            //使用httpClient发送https请求调用微信支付统一下单API
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            httpClient.setHttps(true);  //设置为https请求
            httpClient.setXmlParam(xml);  //设置请求参数（xml）
            httpClient.post();  //发送请求方式为post

            //获取返回值(xml)
            String content = httpClient.getContent();
            System.out.println(content);
            //将xml转化为map
            Map<String, String> stringMap = WXPayUtil.xmlToMap(content);

            HashMap<String, String> resultMap = new HashMap<>();
            //获取前端需要的返回值（code_url,金额，订单号）
            resultMap.put("code_url",stringMap.get("code_url"));
            resultMap.put("out_trade_no",out_trade_no);
            resultMap.put("total_fee",money);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询订单支付状态（调用微信支付的查询订单API）
     * @param out_trade_no
     * @return
     */
    @Override
    public Map<String, String> queryPayStatus(String out_trade_no) {

        try {
            //设置请求参数
            HashMap<String, String> map = new HashMap<>();
            map.put("appid",appid);
            map.put("mch_id",partner);
            map.put("out_trade_no",out_trade_no);
            map.put("nonce_str",WXPayUtil.generateNonceStr());
            //将map转化为xml
            String xml = WXPayUtil.generateSignedXml(map, partnerkey);
            //发送https请求，调用微信支付系统的订单查询
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setXmlParam(xml);
            httpClient.setHttps(true);
            httpClient.post();
            //获取返回值
            String content = httpClient.getContent();
            //将xml转化为map
            Map<String, String> stringMap = WXPayUtil.xmlToMap(content);
            return stringMap;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
